import React, {PureComponent} from 'react';
import {connect} from 'dva';
import {
    Form, Input, Button, Card,Radio
} from 'antd';
import {message} from "antd/lib/index";

const FormItem = Form.Item;
const RadioGroup = Radio.Group;

@connect(state => ({
    submitting: state.adAddress.loading,
}))
@Form.create()
export default class AdAddressEdit extends PureComponent {

    handleParentModalVisible = (e) => {
        this.props.handleModalVisible();
    }

    handleSubmit = (editType,du_id,e) => {
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                this.props.dispatch({
                    type: `adAddressEditAndAdd/${editType}`,
                    payload: {...values,du_id: du_id},
                    callback: (response) => {
                        if (response.result === 0) {
                            message.success('提交成功');
                            this.props.handleModalVisible();
                            this.props.handleModalSuccess(this.props.pagination);
                        } else {
                            message.success('提交失败');
                        }
                    }
                });
            }
        });
    }

    render() {
        const {submitting, record, editType, typeSList} = this.props;
        const {getFieldDecorator, getFieldValue} = this.props.form;
        const formInitValue = editType === 'add' ? '' : record;
        const du_id = editType === 'add' ? '' : record.du_id;
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 7},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 12},
                md: {span: 10},
            },
        };
        const submitFormLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 10, offset: 7},
            },
        };

        return (
            <Card bordered={false}>
                <Form
                    onSubmit={this.handleSubmit}
                    hideRequiredMark
                >
                    <FormItem
                        {...formItemLayout}
                        label="转入地址"
                    >
                        {getFieldDecorator('detail_url', {
                            initialValue:  formInitValue.detail_url || '',
                            rules: [{
                                required: true, message: '请输入广告推荐转入地址',
                            }, {
                                max: 1024, message:'最大字段长度为1024',
                            }
                            ],
                        })(
                            <Input placeholder="广告推荐转入地址"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="备注"
                    >
                        {getFieldDecorator('remark', {
                            initialValue:  formInitValue.remark || '',
                            rules: [{
                                required: true, message: '请输入广告推荐转入地址的备注',
                            }, {
                                max: 1024, message:'最大字段长度为1024',
                            }
                            ],
                        })(
                            <Input placeholder="转入地址的备注"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="状态"
                    >
                        {getFieldDecorator('status', {
                            initialValue:  !formInitValue.status ? 0 : 1,
                            rules: [{
                                required: true, message: '请选择',
                            }],
                        })(
                            <RadioGroup>
                                <Radio value={1}>在用</Radio>
                                <Radio value={0}>不在用</Radio>
                            </RadioGroup>
                        )}
                    </FormItem>
                    <FormItem {...submitFormLayout} style={{marginTop: 32}}>
                        <Button type="primary" loading={submitting} onClick={this.handleSubmit.bind(this,editType,du_id)}>
                            提交
                        </Button>
                        <Button style={{marginLeft: 8}} onClick={this.handleParentModalVisible}>取消</Button>
                    </FormItem>
                </Form>
            </Card>
        );
    }
}
